package com.htgd.gardenexpo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConIntegralGet;
import com.htgd.gardenexpo.entity.GeConIntegralRatio;
import com.htgd.gardenexpo.service.GeConIntegralRatioService;
import com.htgd.gardenexpo.utils.SecurityParameter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 比例 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-07-01
 */
@RestController
@RequestMapping("/api/integralRatio")
@Api(tags = "积分抵扣比例")
@Slf4j
public class GeConIntegralRatioController {

    @Autowired
    private GeConIntegralRatioService geConIntegralRatioService;

    @PostMapping(value = "/edit")
    @ApiOperation("设置抵扣比例")
    public CommonResult<JSONObject> edit(@RequestBody String s) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            JSONObject jsonObject = JSON.parseObject(s);
            Integer ratio = jsonObject.getInteger("ratio");
            geConIntegralRatioService.edit(ratio);
            result.setCode(200);
            result.setMessage("设置抵扣比例成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/get")
    @ApiOperation("查看抵扣比例")
    @SecurityParameter
    public CommonResult<GeConIntegralRatio> get(){
        CommonResult<GeConIntegralRatio> result = new CommonResult<>();
        try{
            GeConIntegralRatio geConIntegralRatio = geConIntegralRatioService.get();
            result.setData(geConIntegralRatio);
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

}
